package com.example.demo.config;

import feign.Feign;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;

/**
 * 配置 okhttp
 *
 * @author zhaiding
 */
@SpringBootConfiguration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class)
public class OkHttpConfig {

    @Resource
    private ObjectFactory<HttpMessageConverters> messageConvertersObjectFactory;

    /**
     * 解决多文件上传 和 不支持 @RequestBody
     */
    @Bean
    public Encoder encoder() {
        // 解决 MultipartFile 多文件上传
        return new SpringFormEncoder(
                new SpringEncoder(this.messageConvertersObjectFactory));
    }


    /*@Bean
    public Encoder encoder() {
        return new FormEncoder();
    }*/
}
